﻿@namespace AntDesign
@using Microsoft.AspNetCore.Components.Rendering
@inherits Tabs
@inject NavigationManager _navigationManager

@{
    base.BuildRenderTree(__builder);
}

@code{
    internal void RenderPanes(RenderTreeBuilder __builder)
    {
        @if (ReuseTabsService.Pages?.Any() == true)
        {
            @foreach (var item in ReuseTabsService.Pages)
            {
                <TabPane Id="@item.Url" @key="@item.Key" Key="@item.Key" Class="@TabPaneClass" Closable="item.Closable" ForceRender ChildContent="@PaneConent(item)">
                    <TabTemplate>
                        @if (Type == TabType.Line && item.Closable)
                        {
                            <span> @item.Title</span>
                            <Icon Style="margin-right:0;" Type="@IconType.Outline.Close" OnClick="@(()=>{ ClosePage(item.Url);})" />
                        }
                        else
                        {
                            @item.Title
                        }
                    </TabTemplate>
                    <TabContextMenu>
                        <Menu>
                            <MenuItem OnClick="@(()=>{ReuseTabsService.ReloadPage(item.Url);})">@Locale.Reload</MenuItem>
                            <MenuItem Disabled="!item.Closable" OnClick="@(()=>{ReuseTabsService.ClosePage(item.Url);})">@Locale.Close</MenuItem>
                            <MenuItem OnClick="@(()=>ReuseTabsService.CloseAll())">@Locale.CloseAllTabs</MenuItem>
                            <MenuItem OnClick="@(()=>{ ReuseTabsService.CloseOther(item.Url);})">@Locale.CloseAllButThis</MenuItem>
                            @* <MenuItem OnClick="@(()=>{ ReuseTabsService.Pin(item.Url);})">@Locale.PinTab</MenuItem> *@
                        </Menu>
                    </TabContextMenu>
                </TabPane>
            }
        }
    }

    private RenderFragment PaneConent(ReuseTabsPageItem item)
    {
        if (!HidePages && item.Rendered)
        {
            item.Body ??=@<AntDesign.Core.Internal.KeepAlive ChildContent="@Body" ></AntDesign.Core.Internal.KeepAlive> ;

            if (TabPaneTemplate != null)
            {
                return TabPaneTemplate(item);
            }
            else
            {
                return item.Body;
            }
        }

        return null;
    }
}